const http = require('https');
const log = console.log;
const baseURL = 'https://ke.segmentfault.com';
const cheerio = require('cheerio');

const getHTMLPage = () => {
  return new Promise((resolve, reject) => {
    http.get(`${baseURL}/free`, (res) => {
      let html = '';
      res.on('data', chunk => html += chunk)
      res.on('end', () => { resolve(html); })
    })
  })
}

const getCourseInfo = (html) => {
  let $ = cheerio.load(html);
  $('body')
    .find('a.text-reset.stretched-link')
    .each(function () {
      const courseName = $(this).text();
      const courseURL = baseURL + $(this).attr('href');
      log('courseName：', courseName);
      log('courseURL：', courseURL);
    })
}

const main = async () => {
  const html = await getHTMLPage();
  log(html);
  getCourseInfo(html);
}

main();
